package com.anbang.app.ui.frame.study.recycleView

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.*
import com.anbang.app.R
import com.anbang.app.ui.frame.study.recycleView.adapter.CommonItemAdapter
import com.anbang.app.ui.frame.study.recycleView.adapter.ItemViewHolder
import com.anbang.app.ui.frame.study.recycleView.adapter.StaggeredItemViewHolder
import com.anbang.framework.utils.toast
import kotlinx.android.synthetic.main.activity_recycle_view.*

class RecycleViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycle_view)

        val dataList = arrayListOf<String>()

        for (i in 0..59) {
            dataList.add("item$i")
        }

        changeBt.setOnClickListener {
            recycleView.adapter = CommonItemAdapter(dataList) {
                return@CommonItemAdapter ItemViewHolder(layoutInflater.inflate(R.layout.item_recycle_view, it, false))
            }

            recycleView.apply {
                //最后的布尔值是 是否翻转布局
                layoutManager = LinearLayoutManager(this@RecycleViewActivity, RecyclerView.VERTICAL, false)
                itemAnimator = DefaultItemAnimator()
                //分割线
                addItemDecoration(TestDecoration(this@RecycleViewActivity, RecyclerView.VERTICAL).apply {
                    ContextCompat.getDrawable(this@RecycleViewActivity,R.drawable.shape_item_divider)?.let { it1 -> setDrawable(it1) }
                })
            }
            toast("LinearLayout")
        }

        change1Bt.setOnClickListener {
            recycleView.adapter = CommonItemAdapter(dataList) {
                return@CommonItemAdapter ItemViewHolder(layoutInflater.inflate(R.layout.item_recycle_view, it, false))
            }

            recycleView.apply {
                //最后的布尔值是 是否翻转布局
                layoutManager = GridLayoutManager(this@RecycleViewActivity, 3)
                itemAnimator = DefaultItemAnimator()
            }
            toast("Grid")
        }

        change2Bt.setOnClickListener {
            recycleView.adapter = CommonItemAdapter(dataList) {
                return@CommonItemAdapter StaggeredItemViewHolder(layoutInflater.inflate(R.layout.item_recycle_view, it, false))
            }

            recycleView.apply {
                //最后的布尔值是 是否翻转布局
                layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
                itemAnimator = DefaultItemAnimator()
            }
            toast("Staggered")
        }
    }

}
